class DistrictsController < ApplicationController
  include AuthenticatedSystemBrowse
  
  def index
    @districts = District.find(:all, :order => "title")
	if current_user.nil?
	  @district = nil
	else
	  @district = current_user.district
	end
  end
  
  def users
    @district = District.find(params[:id])
    @users = User.paginate_by_district_id params[:id], :include => [ :community, :district ], :page => params[:page]
    @page_title = @district.title + ' - 成员'
  end
  
  def search_users
    if params[:id].nil?
      redirect_back_or_default(:controller => 'browse', :action => 'index')
    else
      @district = District.find(:first, :conditions => ["id = ?", params[:id]])
      if @community.nil?
        redirect_back_or_default(:controller => 'browse', :action => 'index')
      else
        if params[:q].nil? || params[:q].blank?
          redirect_to district_url(@district)
        else
          @users = User.paginate_search(params[:q], {:page => params[:page]}, {:conditions => ["district_id = ?", params[:id]]})
        end
      end
    end
  end
  
  def show
    @district = District.find(params[:id])
    @communities = Community.paginate_by_district_id @district, :include => [ :district ], :page => 1
    @new_users = User.find(:all, :conditions => ["district_id = ?", @district.id], :order => "users.join_community_at desc")
    @page_title = @district.title
    @posts = CommonPost.paginate_by_district_id @district, :include => [:user, :post_category, {:community => :district} ], :page => params[:page], :order => "replied_at desc"
    respond_to do |format|
      format.html do
        @tags = district_tags(@district)
        @like_groups = get_users_like_groups(@district.users)
        @posts = CommonPost.paginate_by_district_id @district, :include => [:user, :post_category, {:community => :district} ], :page => params[:page], :order => "replied_at desc"
      end
      format.rss  do
        @posts = CommonPost.find_all_by_district_id @district, :include => [:user, :post_category, {:community => :district} ], :order => "replied_at desc", :limit => "25"
        render :action => "show.rxml", :layout => false
      end
    end
  end
  
  def sites
    @district = District.find(params[:id], :include => [ :communities ])
    @communities = @district.communities
    
    @sites = Site.find :all, :conditions => ["community_sites.community_id = communities.id and community_sites.site_id = sites.id and communities.id in (#{@communities.collect{|n| n.id}.join(',')})"], :from => "sites, communities, community_sites", :select => "sites.*"
    
    respond_to do |format|
      format.html do
        @tags = district_tags(@district)
        sites_ids = @sites.collect{|n| n.id}.join(',')
        sites_ids = [0] if sites_ids.size < 1
        @posts = SitePost.paginate :conditions => ["posts.site_id in (#{sites_ids})"], :include => [ :site ], :page => params[:page], :order => "replied_at DESC"
      end
      format.rss  do
        sites_ids = @sites.collect{|n| n.id}.join(',')
        sites_ids = [0] if sites_ids.size < 1
        @posts = SitePost.find :all, :conditions => ["posts.site_id in (#{sites_ids})"], :include => [ :site ], :order => "replied_at DESC", :limit => 25
        render :action => "sites.rxml", :layout => false
      end
    end
    @page_title = @district.title + ' - 周边场所'
  end
  
  def forums
    @district = District.find(params[:id], :include => [ :communities ])
    @communities = @district.communities.find(:all, :include => [ :community_forum ])
    
    respond_to do |format|
      format.html do
        @tags = district_tags(@district)
        @topics = Topic.paginate :conditions => ["topics.forum_id in (#{@communities.collect{|n| n.community_forum.id}.join(',')})"], :include => [ :user, :forum ], :page => params[:page], :order => "replied_at DESC"
      end
      format.rss do
        @topics = Topic.find :all, :conditions => ["topics.forum_id in (#{@communities.collect{|n| n.community_forum.id}.join(',')})"], :include => [ :user, :forum ], :order => "replied_at DESC", :limit => 25
        render :action => "forums.rxml", :layout => false
      end
    end
    @page_title = @district.title + ' - 论坛'
  end
  
  def ajaxsmallshowcomm
    @district = District.find(params[:id])
    @communities = Community.paginate_by_district_id @district.id, :include => [ :district ], :page => params[:page]
    render :partial => "smallcommlist", :object => @communities
  end
  
  def ajaxbigshowcomm
    @district = District.find(params[:id])
    @communities = Community.paginate_by_district_id @district.id, :include => [ :district ], :page => params[:page]
    render :partial => "bigcommlist", :object => @communities
  end
  
end
